package org.grottarossa.domain;

import java.util.Date;
import java.util.List;

import javax.jdo.annotations.Cacheable;
import javax.jdo.annotations.FetchGroup;
import javax.jdo.annotations.IdGeneratorStrategy;
import javax.jdo.annotations.IdentityType;
import javax.jdo.annotations.NullValue;
import javax.jdo.annotations.PersistenceCapable;
import javax.jdo.annotations.Persistent;
import javax.jdo.annotations.PrimaryKey;
import javax.jdo.annotations.Serialized;

import org.apache.log4j.Logger;

@PersistenceCapable(identityType=IdentityType.DATASTORE,detachable="true")
@FetchGroup(name="field-fetch-group", members={
		@Persistent(name="name"),
		@Persistent(name="description")	,	
		@Persistent(name="field_id")	,	
		@Persistent(name="value")	
})
public class Field extends MainObject
{
	Logger logger = Logger.getLogger(CaseFolder.class.getName());
	
	@PrimaryKey
	@Persistent(valueStrategy=IdGeneratorStrategy.INCREMENT)
	@Serialized
	private String field_id;
	
	@Persistent
	@Cacheable("true")
	private String name;
	
	@Persistent
	@Cacheable("true")
	private String description;
	
	@Persistent
	@Cacheable("true")
	private boolean isMultivalue;
	
	@Persistent
	@Cacheable("true")
	private boolean isRequired;
	
	@Persistent(types={String.class,Integer.class,Double.class,Long.class,Date.class},nullValue=NullValue.EXCEPTION)
	@Cacheable("true")
	private List<Object> value;
		
	@Override
	public String toString() {
		return "id:"+field_id+" name:"+name+" description:"+description + " value"+value;
	}
	public void setName(String name) {
		this.name = name;
	}

	public String getName() {
		return name;
	}

	public void setMultivalue(boolean isMultivalue) {
		this.isMultivalue = isMultivalue;
	}

	public boolean isMultivalue() {
		return (value.size()>1);
	}

	public void setValue(List<Object> value) {
		this.value = value;
	}

	public List<Object> getValue() {
		return value;
	}

	public String getId() {
		return this.field_id;
	}

	/**
	 * @param description the description to set
	 */
	public void setDescription(String description) {
		this.description = description;
	}

	/**
	 * @return the description
	 */
	public String getDescription() {
		return description;
	}

	/**
	 * @param isRequired the isRequired to set
	 */
	public void setRequired(boolean isRequired) {
		this.isRequired = isRequired;
	}

	/**
	 * @return the isRequired
	 */
	public boolean isRequired() {
		return isRequired;
	}
	
	@Override
	public boolean equals(Object obj) 
	{		
		return compare(obj);
	}
	
	@Override
	public Object cloneObj() 
	{		
		return cloneObj(Field.class);
	}
	
	@Override
	public void update(Object obj) 
	{
		update(obj, Field.class);
	}	
}